using System;
using System.Collections.Generic;
using System.Text;
using Dida;
using Dida.Dbg;
using Dida.Dxml;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace GameplayWorld
{
	class TestPawn : Pawn
	{
		public TestPawn()
			: base(Pawn.GetStackedSpheres(.5f,2,3))
		{
			Pos = new Vector3D(0, 2, 3.7f);
		}

		public override void Update(float timeDelta)
		{
			Move(new Vector3D(0, -timeDelta, 0), 0, 1, CollisionResponse.Stop);
		}
	}

	class Program
	{
		static World world = new World();
		static ObjectManager objectManager;

		static void Init()
		{
			UnmanagedResourceManager.Init(UnmanagedResMgrFlags.None);
			Geometry.Init();
			Animation.Init();
			WorldManagement.Init();
			SdlBinding.Init(640, 480);
			DidaXml.Finish();

			WorldXmlCompiler compiler = new WorldXmlCompiler(world);
			compiler.Load("../../../../../assets/CollisionTest/CollisionTestLevel.ddm");
			objectManager = compiler.Compile();

			TestPawn pawn = new TestPawn();
			world.AddActor(pawn);

			world.Start();
		}

		static void Free()
		{
			world.Stop();

			SdlBinding.Free();
			WorldManagement.Free();
			Animation.Free();
			Geometry.Free();
			UnmanagedResourceManager.Free();
		}

		static void OnIdle(float timeDelta)
		{
			world.Update(timeDelta);

			Viewpoint viewpt = Viewpoint.CreateOverview(new Vector3D(0, 0, 0),
				Rotation.Deg2Rad(30), Rotation.Deg2Rad(-30), 10, 1);
			viewpt.CalcMatrices(640 / 480.0f);
			DebugDraw.BeginDrawing(viewpt);
			world.DrawDebug(DebugDraw.DebugDrawTarget);
			DebugDraw.EndDrawing();
		}

		static void Main(string[] args)
		{
			try
			{
				Init();

				SdlBinding.Run(OnIdle);

				Free();
			}
			catch (Exception err)
			{
				Free();
				MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
			}
		}
	}
}
